var moes;
var areas;
var dataSource;
var countRows = 1;

$(document).ready(function () {
    dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.urlRead,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                fields: {
                    nomina: { type: 'string' },
                    name: { type: 'string' },
                    moe: { type: 'string' },
                    area: { type: 'string' },
                    line: { type: 'string' },
                    sche: { type: 'string' },
                    supervisor: { type: 'string' },
                    work: { type: 'string' },
                    start: { type: 'date' },
                    life: { type: 'date' },
                    end: { type: 'date' },
                    startExt: { type: 'date' },
                    endExt: { type: 'date' },
                    comments: { type: 'string' }
                }
            }
        }
    });

    moes = $("#moes").kendoDropDownList({
        filter: "contains",
        placeholder: "Elige el MOE...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            type: "json",
            transport: {
                read: appBosch.urlMoes
            }
        }
    }).data("kendoDropDownList");

    areas = $("#areas").kendoDropDownList({
        autoBind: false,
        cascadeFrom: "moes",
        filter: "contains",
        placeholder: "Elige el Área...",
        dataTextField: "text",
        dataValueField: "value",
        cascade: readData,
        dataSource: {
            serverFiltering: true,
            type: "json",
            transport: {
                read: appBosch.urlAreas
            }
        }
    }).data("kendoDropDownList");

    $('#certSuccess').on('change', readData);

    $("#grid").kendoGrid({
        toolbar: [
            {
                name: "excel"
            },
            {
                name: "pdf"
            }
        ],
        excel: {
            allPages: true,
            fileName: "P0007Q Anexo 20 Control de pasaportes.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "P0007Q Anexo 20 Control de pasaportes.pdf"
        },
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: true,
        height: $(window).height() - 350,
        dataBound: function () {
            countRows = 1;
        },
        dataSource: dataSource,
        columns: [
            {title: "#", template: '#=countRows++#', width: 40 },
            {field: "nomina", title: "Nómina", width: 100 },
            {field: "name", title: "Nombre", width: 150 },
            {field: "moe", title: "MOE", width: 100 },
            {field: "area", title: "Área", width: 100 },
            {field: "line", title: "Línea", width: 100 },
            {field: "sche", title: "Turno", width: 50 },
            {field: "supervisor", title: "Supervisor", width: 100 },
            {field: "work", title: "Operación", width: 150 },
            {
                field: "start",
                title: "Inicio del proceso",
                width: 100,
                template: '<span title="#=kendo.toString(new Date(start), "D")#">#=kendo.toString(new Date(start), "d")#</span>'
            },
            {
                field: "end",
                title: "Fin de proceso",
                width: 100,
                template: '<span title="#=kendo.toString(new Date(end), "D")#">#=kendo.toString(new Date(end), "d")#</span>'
            },
            {
                field: "life",
                title: "Vigencia",
                width: 100,
                template: '<span title="#=kendo.toString(new Date(life), "D")#">#=kendo.toString(new Date(life), "d")#</span>'
            },
            {
                field: "startExt",
                title: "Inicio de extención",
                width: 100,
                template: '<span title="#=kendo.toString(new Date(startExt), "D")#">#=kendo.toString(new Date(startExt), "d")#</span>'
            },
            {
                field: "endExt",
                title: "Fin de extención",
                width: 100,
                template: '<span title="#=kendo.toString(new Date(endExt), "D")#">#=kendo.toString(new Date(endExt), "d")#</span>'
            },
            {field: "comments", title: "Comentarios", width: 150 }
        ]
    });
});

function readData() {
    dataSource.transport.options.read.url = appBosch.urlRead + '/' + areas.value() + '/' + $('#certSuccess').is(":checked");
    dataSource.read();
}

